<?php
class update_permission
{
    public $layout = NULL;
    public $user_id = NULL;
    public function __construct()
    {
        $this->view = new system_view();
        $this->model = new model__update_permission_model();
        $this->rt = new handle_route();
    }
    public function index_controller()
    {
        $strHtml = $this->view->view_display('index');
        $msg = '';
        if(isset($_POST['update']))
        {
            $update_permission = $this->update_permission();
            if($update_permission == 0)
            {
                $msg = 'Permission đã cập nhật thành công: ';
            }else{
                $msg = 'Có lỗi xẩy ra trong quá trình update: ';
            }
            
        }
        
        $strHtml = $this->view->view_assign($strHtml,array('msg'=>$msg));
        return $strHtml;
    }
    
    public function update_permission()
    {
        $list_com_act_com = $this->model->md_pu_get_list_action_com();
        $list_action = $this->model->md_pu_get_list_action();
        $list_com = $this->model->md_pu_get_list_com();
        
        
        if(empty($list_com_act_com) || empty($list_action) || empty($list_com))
        {
            return 1;
        }else{
            $list_action_array = array();
            foreach($list_action as $action)
            {
                $list_action_array[$action['ID']] = $action['ActName'];
            }
            
            $list_com_array = array();
            foreach($list_com as $com)
            {
                $list_com_array[$com['ID']] = $com['Com'];
            }
            
            if(!empty($list_com_act_com))
            {
                foreach($list_com_act_com as $com_act)
                {
                    $name_permission = $list_com_array[$com_act['ComID']] . '_' . $list_action_array[$com_act['ActID']];
                    $data = array();
                    $data['PremissName'] = $name_permission;
                    $data['CID'] = $com_act['ComID'];
                    $data['Action'] = $list_action_array[$com_act['ActID']];
                    $data['Status'] = 'true';
                    $check_permission = $this->model->md_pu_check_permission($data['CID'],$data['Action']);
                    if(empty($check_permission))
                    {
                        $this->model->md_pu_insert($data);
                    }
                }
            }
            return 0;
            
        }
        
    }
}